package com.goods.web.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.goods.pojo.CpspBean;
import com.goods.service.ICpspService;
import com.goods.util.CommonTools;
import com.somtacode.core.base.result.ResponseDataResult;
import com.somtacode.core.utils.CommonUtil;

@RestController
@RequestMapping("/cpsp")
public class CpspController {
	
	@Autowired 
	private ICpspService cpspService; 
	
	
		/**
		 * 新增运营商
		 * @param cpsp
		 * @return
		 * @throws Exception
		 */
		@PostMapping("/addCpsp")
	    public ResponseDataResult addCpsp(CpspBean cpsp) throws Exception{
	        if(CommonUtil.isNullOrEmpty(cpsp)){
	    		return ResponseDataResult.setErrorResponseResult("cpsp不能为空");
	    	}
	        cpsp.setCreateDate(new Date());
	        return cpspService.addCpsp(cpsp);
	    }
	
	
		/**
		 * 删除运营商
		 * @param id
		 * @return
		 * @throws Exception
		 */
		@GetMapping("/deleteCpsp")
	    public ResponseDataResult deleteCpsp(Integer id) throws Exception{
	    	if(CommonUtil.isNullOrEmpty(id)){
	    		return ResponseDataResult.setErrorResponseResult("id不能为空");
	    	}
	    	try {
	    		cpspService.deleteCpsp(id);
			} catch (Exception e) {
				
				e.printStackTrace();
			}
	    	return ResponseDataResult.setResponseResult();
	    }


		/**
		 * 修改运营商
		 * @param cpsp
		 * @return
		 * @throws Exception
		 */
		@PostMapping("/updateCpsp")
		public ResponseDataResult updateCpsp(CpspBean cpsp) throws Exception{
			if(CommonUtil.isNullOrEmpty(cpsp)){
				return ResponseDataResult.setErrorResponseResult("cpsp不能为空");
			}
			return cpspService.updateCpsp(cpsp);
		}
		
		/**
		 * 根据id获取运营商
		 * @param id
		 * @return
		 * @throws Exception
		 */
		@GetMapping("/getCpsp")
	    public ResponseDataResult getCpsp(Integer id) throws Exception{
			List<CpspBean> cpsp = null;
			try {
				cpsp = cpspService.getCpsp(id);
	    		if(CommonUtil.isNullOrEmpty(cpsp)){
	        		return ResponseDataResult.setErrorResponseResult("cpsp不存在，请重新输入");
	        	}
			} catch (Exception e) {
				e.printStackTrace();
			}
	    	return ResponseDataResult.setResponseResult(cpsp);
	    }
		
		/**
		 * 获取运营商列表
		 * @param p
		 * @param size
		 * @param name
		 * @return
		 * @throws Exception
		 */
		@GetMapping("/listCpsp")
		public List<CpspBean> listCpsp(Integer p,Integer size,String name) throws Exception {
			p = p == null ? 1 : p;
			size = size == null ? 20 :size;
			CpspBean cpsp = new CpspBean();
			if(!CommonTools.isNullOrEmpty(name)){
				cpsp.setName(name);
			}
			PageInfo<CpspBean> page = cpspService.listCpsp(p, size,cpsp);
			return page.getList();
		}
		
}
